//
//  UIImageView+downloadImage.m
//  Sekiss
//
//  Created by Alex on 15-3-4.
//  Copyright (c) 2015年 Alex. All rights reserved.
//

#import "UIImageView+downloadImage.h"

@implementation UIImageView (downloadImage)

-(void)setImageWithUrl:(NSString*)url
{
    [self setImageWithUrl:url placerHolder:nil];
}

/**
 *  下载网络图片
 *
 *  @param url   图片url
 *  @param photo 默认图片
 */
-(void)setImageWithUrl:(NSString*)url placerHolder:(UIImage*)photo;
{
    if([url rangeOfString:@"http"].location==NSNotFound)
    {
        url=[NSString stringWithFormat:@"%@%@",prefix,url];
    }
    /*
    if([url hasPrefix:prefix] && [url hasSuffix:@".jpg"])
    {
        url=[url stringByReplacingOccurrencesOfString:@".jpg" withString:@"_m.jpg"];
    }
    if([url hasPrefix:prefix] && [url hasSuffix:@".mp4"])
    {
        url=[url stringByReplacingOccurrencesOfString:@".mp4" withString:@"_c.jpg"];
    }
    */
    [[DKImageSqlite shareImageSqlite] loadImage:url
                                    placeHolder:photo
                                         target:self
                                        process:nil
                                 enableDownload:YES
                                     completion:nil];
}

/**
 *  利用金山云裁剪图片的下载
 *
 *  @param url  图片url
 *  @param size 尺寸
 */
- (void)setImageUrl:(NSString *)url toSize:(CGSize)size
{
    if([url rangeOfString:@"http"].location==NSNotFound)
    {
        url=[NSString stringWithFormat:@"%@%@",prefix,url];
    }
    
    if([url hasPrefix:prefix] && [url hasSuffix:@".mp4"])
    {
        url=[url stringByReplacingOccurrencesOfString:@".mp4" withString:@"_c.jpg"];
    }
    
    url=[NSString stringWithFormat:@"%@@base@tag=imgScale&m=1&w=%.0f&h=%.0f",url,size.width*[UIScreen mainScreen].scale,size.height*[UIScreen mainScreen].scale];
    [[DKImageSqlite shareImageSqlite] loadImage:url
                                    placeHolder:nil
                                         target:self
                                        process:nil
                                 enableDownload:YES
                                     completion:nil];
}

/**
 *  利用金山云裁剪到最小尺寸图片
 *
 *  @param url 图片url
 */
- (void)setImageUrlToMiniSize:(NSString *)url
{
    [self setImageUrl:url toSize:CGSizeMake(50, 50)];
}

/**
 *  利用金山云裁减到中间尺寸
 *
 *  @param url 图片url
 */
- (void)setImageUrlToMiddleSize:(NSString *)url
{
    [self setImageUrl:url toSize:CGSizeMake(150, 150)];
}


+(void)downloadImageWithUrl:(NSString*)url
                 Completion:(DKImageSqliteBlock)block
{
    [UIImageView downloadImageWithUrl:url
                          forOriginal:NO
                           Completion:block];
}

/**
 *  下载网络图片
 *
 *  @param url        图片url
 *  @param isOriginal 是否使用原图
 *  @param block      下载完成后的回调
 */
+ (void)downloadImageWithUrl:(NSString *)url
                 forOriginal:(BOOL)isOriginal
                  Completion:(DKImageSqliteBlock)block
{
    if(url.length==0)
    {
        block(nil);
        return;
    }
    if([url rangeOfString:@"http"].location==NSNotFound)
    {
        url=[NSString stringWithFormat:@"%@%@",prefix,url];
    }
    
    if(!isOriginal)
    {
        /*
        if([url hasPrefix:prefix] && [url hasSuffix:@".jpg"])
        {
            url=[url stringByReplacingOccurrencesOfString:@".jpg" withString:@"_m.jpg"];
        }
        if([url hasPrefix:prefix] && [url hasSuffix:@".mp4"])
        {
            url=[url stringByReplacingOccurrencesOfString:@".mp4" withString:@"_c.jpg"];
        }
         */
    }
    [[DKImageSqlite shareImageSqlite] loadImage:url
                                    placeHolder:nil
                                         target:nil
                                        process:nil
                                 enableDownload:YES
                                     completion:block];
}


/**
 *  利用金山云的裁剪下载图片
 *
 *  @param url   图片url
 *  @param size  尺寸
 *  @param block 下载完成后的回调
 */
+ (void)downloadImageUrl:(NSString *)url
                  toSize:(CGSize)size
              completion:(DKImageSqliteBlock)block
{
    if(url.length==0)
    {
        block(nil);
        return;
    }
    
    if([url rangeOfString:@"http"].location==NSNotFound)
    {
        url=[NSString stringWithFormat:@"%@%@",prefix,url];
    }
    
    if([url hasPrefix:prefix] && [url hasSuffix:@".mp4"])
    {
        url=[url stringByReplacingOccurrencesOfString:@".mp4" withString:@"_c.jpg"];
    }
    
    url=[NSString stringWithFormat:@"%@@base@tag=imgScale&m=1&w=%.0f&h=%.0f",url,size.width*[UIScreen mainScreen].scale,size.height*[UIScreen mainScreen].scale];
    [[DKImageSqlite shareImageSqlite] loadImage:url
                                    placeHolder:nil
                                         target:nil
                                        process:nil
                                 enableDownload:YES
                                     completion:block];
}



/**
 *  利用金山云裁剪生成新url
 *
 *  @param url  图拍url
 *  @param size 尺寸
 *
 *  @return 转换后的url
 */
+ (NSString *)convertUrl:(NSString *)url toSize:(CGSize)size
{
    if([url rangeOfString:@"http"].location==NSNotFound)
    {
        url=[NSString stringWithFormat:@"%@%@",prefix,url];
    }
    
    if([url hasPrefix:prefix] && [url hasSuffix:@".mp4"])
    {
        url=[url stringByReplacingOccurrencesOfString:@".mp4" withString:@"_c.jpg"];
    }
    
    url=[NSString stringWithFormat:@"%@@base@tag=imgScale&m=1&w=%.0f&h=%.0f",url,size.width*[UIScreen mainScreen].scale,size.height*[UIScreen mainScreen].scale];
    return url;
}


+(void)clearData
{
    [DKImageSqlite clearData];
}

@end
